define([
    'zepto',
    'underscore',
    'backbone',
    "text!templates/wait.html",
    "wTools"

], function ($, _, Backbone, template) {
    return Backbone.View.extend({
        $el: $main,
        el: $main0,
        template: _.template(template),
        events: {
            "tap #select":"select"
        },
        initialize: function () {
            $("#head_back").hide();
            $("#home").html("");
            this.render();
            wLoading.start();
            var timeSix = 6;
            var IntervalNo = setInterval(function () {
                timeSix--;
                if (timeSix == 0) {
                    wLoading.stop();
                    clearInterval(IntervalNo);
                    $("#showMsg").html("请手动查询,或联系客服");
                    $("#select").removeClass("btn_gray").addClass("btn");
                } else {
                    wAjaxFormNoBg("getPayStatus", "", {"sourceVoucherNo": wData.orderInfo.sourceVoucherNo}, function (result) {
                        if (result.data.tradeInfoList[0].status == "401" || result.data.tradeInfoList[0].status == "301") {
                            wLoading.stop();
                            clearInterval(IntervalNo);
                            location.hash = "success";

                        } else if (result.data.tradeInfoList[0].status == "100") {
                            //交易进行中
                        } else if (result.data.tradeInfoList[0].status == "999") {
                            wLoading.stop();
                            clearInterval(IntervalNo);
                            $("#showMsg").html("交易过期,请联系客服人工查询");
                        } else {
                            wLoading.stop();
                            clearInterval(IntervalNo);
                            wData.errorMessage = "未知失败原因,请联系客服";
                            location.hash = "fail";
                        }
                    });
                }
            }, 1500);
        },
        select: function () {
            if($("#select").hasClass("btn")) {
                wAjaxForm("getPayStatus", "", {"sourceVoucherNo": wData.orderInfo.sourceVoucherNo}, function (result) {
                    $("#select").removeClass("btn").addClass("btn_gray").html("5s后可查询");
                    var timeSelect = 5;
                    var IntervalNoSelect = setInterval(function () {
                        timeSelect--;
                        if (timeSelect == 0) {
                            clearInterval(IntervalNoSelect);
                            $("#select").removeClass("btn_gray").addClass("btn").html("查询交易结果");
                        } else {
                            $("#select").html(timeSelect + "s后可查询");
                        }
                    }, 1000);
                    if (result.data.tradeInfoList[0].status == "401" || result.data.tradeInfoList[0].status == "301") {
                        location.hash = "success";

                    } else if (result.data.tradeInfoList[0].status == "100") {
                        //交易进行中
                    } else if (result.data.tradeInfoList[0].status == "999") {
                        $("#showMsg").html("交易过期,请联系客服人工查询");
                        $("#select").removeClass("btn").addClass("btn_gray").html("查询交易结果");
                    } else {
                        wData.errorMessage = "未知失败原因,请联系客服";
                        location.hash = "fail";
                    }
                });

            }
        },
        back: function () {
            //history.back();
        },
        render: function (data) {
            var html = this.template(data);
            this.$el.html(html);
        },
        destroy: function () {
            this.undelegateEvents();
            this.$el.html("");
            $("#head_back").show();
            delete this;
        }
    });
});